package org.jeecg.modules.demo.dz.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.dz.constant.DzConstant;
import org.jeecg.modules.demo.dz.entity.DzChooseItem;
import org.jeecg.modules.demo.dz.service.IDzChooseItemService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 选材管理
 * @Author: jack
 * @Date:   2024-11-24
 * @Version: V1.0
 */
@Api(tags="选材管理")
@RestController
@RequestMapping("/dz/dzChooseItem")
@Slf4j
public class DzChooseItemController extends JeecgController<DzChooseItem, IDzChooseItemService> {
	@Autowired
	private IDzChooseItemService dzChooseItemService;
	
	/**
	 * 分页列表查询
	 *
	 * @param dzChooseItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "选材管理-分页列表查询")
	@ApiOperation(value="选材管理-分页列表查询", notes="选材管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DzChooseItem>> queryPageList(DzChooseItem dzChooseItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        QueryWrapper<DzChooseItem> queryWrapper = QueryGenerator.initQueryWrapper(dzChooseItem, req.getParameterMap());
		Page<DzChooseItem> page = new Page<DzChooseItem>(pageNo, pageSize);
		IPage<DzChooseItem> pageList = dzChooseItemService.page(page, queryWrapper);
		/*pageList.getRecords().stream().forEach(item -> {
			item.setFileUrl(StringUtils.isNotEmpty(item.getFileUrl()) ? item.getFileUrl()+"?x-oss-process="+DzConstant.w_500 : null);
			item.setBackgroundUrl(StringUtils.isNotEmpty(item.getBackgroundUrl()) ? item.getBackgroundUrl()+"?x-oss-process="+DzConstant.w_500 : null);
		});*/
		return Result.OK(pageList);
	}

	 @ApiOperation(value="选材管理-小程序", notes="选材管理-分页列表查询")
	 @GetMapping(value = "/miniList")
	 public Result<IPage<DzChooseItem>> miniList(DzChooseItem dzChooseItem,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="100") Integer pageSize,
													  HttpServletRequest req) {
		 QueryWrapper<DzChooseItem> queryWrapper = QueryGenerator.initQueryWrapper(dzChooseItem, req.getParameterMap());
		 Page<DzChooseItem> page = new Page<DzChooseItem>(pageNo, pageSize);
		 IPage<DzChooseItem> pageList = dzChooseItemService.page(page, queryWrapper);
		 pageList.getRecords().stream().forEach(item -> {
			item.setFileUrl(StringUtils.isNotEmpty(item.getFileUrl()) ? item.getFileUrl()+"?x-oss-process="+DzConstant.w_500 : null);
			item.setBackgroundUrl(StringUtils.isNotEmpty(item.getBackgroundUrl()) ? item.getBackgroundUrl()+"?x-oss-process="+DzConstant.w_500 : null);
		 });
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param dzChooseItem
	 * @return
	 */
	@AutoLog(value = "选材管理-添加")
	@ApiOperation(value="选材管理-添加", notes="选材管理-添加")
	@RequiresPermissions("dz:dz_choose_item:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DzChooseItem dzChooseItem) {
		dzChooseItemService.save(dzChooseItem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param dzChooseItem
	 * @return
	 */
	@AutoLog(value = "选材管理-编辑")
	@ApiOperation(value="选材管理-编辑", notes="选材管理-编辑")
	@RequiresPermissions("dz:dz_choose_item:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DzChooseItem dzChooseItem) {
		dzChooseItemService.updateById(dzChooseItem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "选材管理-通过id删除")
	@ApiOperation(value="选材管理-通过id删除", notes="选材管理-通过id删除")
	@RequiresPermissions("dz:dz_choose_item:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		dzChooseItemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "选材管理-批量删除")
	@ApiOperation(value="选材管理-批量删除", notes="选材管理-批量删除")
	@RequiresPermissions("dz:dz_choose_item:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.dzChooseItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "选材管理-通过id查询")
	@ApiOperation(value="选材管理-通过id查询", notes="选材管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DzChooseItem> queryById(@RequestParam(name="id",required=true) String id) {
		DzChooseItem dzChooseItem = dzChooseItemService.getById(id);
		if(dzChooseItem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(dzChooseItem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param dzChooseItem
    */
    @RequiresPermissions("dz:dz_choose_item:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DzChooseItem dzChooseItem) {
        return super.exportXls(request, dzChooseItem, DzChooseItem.class, "选材管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("dz:dz_choose_item:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DzChooseItem.class);
    }

}
